var indiceAtual = 1;
var ytplayer;
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };

function onYouTubePlayerReady(playerId) {
	//alert("ready");
	ytplayer = document.getElementById("myytplayer");
	ytplayer.addEventListener("onStateChange", "statusAlterado");
	marcar(lista[0].videoId);
}

function statusAlterado(newState) {
	//alert(newState);
	if(parseInt(newState)==0){//acabou o video
		proximo();
	}
}
function cadastrar(f){
	var codigo;
	var inicio="watch?v=";
	var fim="&";
	var ok=false;
	codigo = f.youtubeUrl.value;
	var i = codigo.indexOf(inicio);
	if(i!=-1){//achou o inicio
		var fim = codigo.indexOf(fim,i);
		var videoId="";
		if(fim!=-1){
			videoId = codigo.substring(i+inicio.length,fim); 
		}else{
			videoId = codigo.substring(i+inicio.length); 
		}
		if(videoId!=""){
			salvarAjax(videoId);
			ok=true;
		}
	}
	if(!ok){
		alert("Erro: sem id.");
	}
	return false;
}
function Video(nome,id){
	this.titulo = nome;
	this.videoId = id;
}
function salvarAjax(videoId){
	$.ajax({
		type: "POST",
		url: "incluirVideo.php",
		data: "videoId=" + videoId,
		success: function(msg){
			var res = eval("(" + msg + ")");
			alert(res.msg);
			if(res.ok){
				//atualizar o titulo
				$("#" + videoId + ">a.title").text(res.video.titulo);
				criarLi(videoId,res.video.titulo);
				lista.push(new Video(res.video.titulo, videoId));
			}
		}
	});
}
function apagar(videoId){
	$.ajax({
		type: "POST",
		url: "apagarVideo.php",
		data: "videoId=" + videoId,
		success: function(msg){
			alert( "Data Deleted: " + msg );
		}
	});
	$("#"+videoId).remove();
}
function play(videoId){
	ytplayer.loadVideoById(videoId);
	marcar(videoId);
	//procurar o i atual
	for(i=0;i<lista.length;i++){
		if(lista[i].videoId==videoId){
			indiceAtual = i+1;
			break;
		}
	}
	return false;
}
function carregarLista(){
	var i;
	for(i=0;i<lista.length;i++){
		criarLi(lista[i].videoId,lista[i].titulo);
	}
}
function criarLi(videoId,titulo){
	$("#lista").append("<li id=\""+videoId+"\"><a href=\"#\" onclick=\"return play('"+videoId+"')\" class=\"title\">"+titulo+"</a> <a href=\"javascript:apagar('" + videoId + "');\">[X]</a></li>");
}
function marcar(videoId){
	var ul = document.getElementById("lista");
	var lis = ul.getElementsByTagName("li");
	for(var i=0;i<lis.length;i++){
		lis[i]["style"].backgroundColor="#fff";
	}
	var li = document.getElementById(videoId);
	li["style"].backgroundColor="#eee";
}
function proximo(){
	if(indiceAtual>=lista.length){
		indiceAtual=0;
	}
	ytplayer.loadVideoById(lista[indiceAtual].videoId);
	marcar(lista[indiceAtual].videoId);
	indiceAtual++;
}
function newUsuario(frm){
	$.post("cadastro.php", ({nome: frm.nome.value, email: frm.email.value, senha: frm.senha.value}),
		function(data){
			alert("Data Loaded: " + data);
		}
	);
	return false;
}
function login(frm){
	$.post("login.php", {email: frm.email.value, senha: frm.senha.value},
		function(data){
			alert("Data Loaded: " + data);
		}
	);
	return false;
}
$(document).ready(function(){
	carregarLista();
	swfobject.embedSWF("http://www.youtube.com/v/" + lista[0].videoId + "&enablejsapi=1&playerapiid=ytplayer&autoplay=1&rel=0", 
				   "ytapiplayer", "425", "356", "8", null, null, params, atts);
});